// pages/home-video/home-video.js
import {reqgetVideoList} from "../../service/homeVideo"
Page({
  data: {
    videoList: [],
    offset: 0,
    hasMore: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getVideoList()
  },

   /**
   * 发送网络请求
   */
  async getVideoList(){
    const res = await reqgetVideoList(this.data.offset)
    const newVideoList = [...this.data.videoList, ...res.data]
    this.data.offset = this.data.videoList.length
    this.setData({videoList: newVideoList})
    this.data.hasMore = res.hasMore
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    // 1、清空data数据
    this.setData({ videoList: [] })
    this.data.offset = 0
    this.data.hasMore = true

    // 2、重新请求数据
    // this.getVideoList().then(() => {
    //   // 3.停止下拉刷新(请求成功后)
    //   wx.stopPullDownRefresh()
    // })
    await this.getVideoList()

    // 3.停止下拉刷新(请求成功后)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(!this.data.hasMore) return
    this.getVideoList()
  },
})